package com.china08.yxyapi.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class DisposeCkContentUtil {

    public static String dispose(String str) {
        Document document = Jsoup.parse(str);

        // div首行缩进2em
        Elements div_elements = document.select("div");
        for (Element d_element : div_elements) {
            String style = d_element.attr("style");
            d_element.attr("style", "text-indent:2em;" + style);
        }

        // ##
        // p标签处理
        Elements p_elements = document.select("p");
        for (Element p_element : p_elements) {
            p_element.html(p_element.html().trim().replace("　", "").replace("&nbsp;", ""));
            // p标签原来的样式
            String style = p_element.attr("style");
            p_element.attr("style", "text-indent:2em;" + style);
            Elements img_elements = p_element.select("img");
            if (img_elements.size() > 0) {
                // img_elements.attr("style", "display:block; margin:10px
                // auto;");
                // img_elements.wrap("<p style='text-align:center;'></p>");
            }
        }
        return document.select("body").html();
    }

    public static String disposeHeight(String str) {
        Document document = Jsoup.parse(str);
        Elements imgElements = document.select("img");
        for (Element imgElement : imgElements) {
            String style = imgElement.attr("style");
            style = style.replaceAll("max-width", "max-width_back");
            style = style.replaceAll("height", "height_back");
            style = style.replaceAll("max-height", "max-height_back");
            imgElement.attr("style", style);
        }
        return document.html();
    }
}
